using UnityEngine;
using System.Collections;

public class InputManager : MonoBehaviour
{
    //public GUIManager guiManager;               //A link to the GUI Manager
    public PlayerManager playerManager;         //A link to the Player Manager

    public bool useTouch = false;				//Use touch based controls

    public LayerMask mask = -1;					//Set input layer mask

    private RaycastHit2D hit;                   //The hit raycast
    private Vector3 inputPos;                   //The position of the input
    private Vector3 tempPos;                    //A temporary position for the buttons
    private Transform button;                   //The triggered button

    //Called at every frame
    void Update()
    {
        if (useTouch)
            GetTouches();
        else
            GetClicks();
    }
    //If playing with mouse
    void GetClicks()
    {
        //Get the position of the input
        inputPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        inputPos.z = 0;

        //Cast a ray
        hit = Physics2D.Raycast(inputPos, new Vector2(0, 0), 0.1f, mask);

        //If we pressed the mouse
        if (Input.GetMouseButtonDown(0))
        {
            //If the ray hit something in the set layer
            if (hit.collider != null)
            {
                ButtonPressed(hit.transform);
            }
            //If the ray did not hit an object
            else
            {
                //Forward the input to the player manager
                playerManager.UpdateInput(true);
            }
        }
        //If we move the mouse
        else if (Input.GetMouseButton(0))
        {
            //If the player hit a button at the last click, and the mouse was moved out of the button's zone
            if (hit.collider == null && button)
                //Release the button
                ButtonReleased();
        }
        //If the click was released
        else if (Input.GetMouseButtonUp(0))
        {
            //If the player hit a button at the last click
            if (button)
                //Release the button
                ButtonReleased();
            else
                playerManager.UpdateInput(false);
        }
    }
    //If playing with touch screen
    void GetTouches()
    {
        //Loop through the touches
        foreach (Touch touch in Input.touches)
        {
            //Get the position of the input
            inputPos = Camera.main.ScreenToWorldPoint(Input.touches[0].position);
            inputPos.z = 0;

            //If a touch has happened
            if (touch.phase == TouchPhase.Began && touch.phase != TouchPhase.Canceled)
            {
                //Cast a ray
                hit = Physics2D.Raycast(inputPos, new Vector2(0, 0), 0.1f, mask);

                //If the ray hit something in the set layer
                if (hit.collider != null)
                    ButtonPressed(hit.transform);
                //If the ray did not hit an object
                else
                {
                    //Forward the input to the player manager
                    playerManager.UpdateInput(true);
                }
            }
            //If the touch has moved
            else if (touch.phase == TouchPhase.Moved)
            {
                //Cast a ray
                hit = Physics2D.Raycast(inputPos, new Vector2(0, 0), 0.1f, mask);

                //If the player hit a button at the last touch, and moved it out of the button's zone
                if (hit.collider == null && button)
                    //Release the button
                    ButtonReleased();
            }
            //If a touch has ended
            else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
            {
                //If the player hit a button at the last touch, relese the button
                if (button)
                    ButtonReleased();
                //Else, notify the player manager
                else
                    playerManager.UpdateInput(false);
            }
        }
    }

    //Moves the selected button down
    void ButtonPressed(Transform b)
    {
        if (b.tag != "Button")
            return;

        //Save the button's reference
        button = b.transform;

        //Move the button down by 0.05
        tempPos = button.position;
        tempPos.y -= 0.05f;
        button.position = tempPos;
    }
    //Moves the button back to it's place, and execute it's event
    void ButtonReleased()
    {
        //Move the button up by 0.05
        tempPos = button.position;
        tempPos.y += 0.05f;
        button.position = tempPos;

        //Notify the GUI manager, and remove the reference to the button
        //guiManager.ButtonPressed(button.gameObject.name);
        button = null;
    }
}
